package com.zjxny.proxy;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.zjxny.model.CustomerInfo;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.erupt.annotation.config.Comment;
import xyz.erupt.annotation.fun.DataProxy;

@Slf4j
@Service
public class CustomerProxy implements DataProxy<CustomerInfo>{

	@Resource
	JdbcTemplate jdbcTemplate;
	
	@Override
	public void beforeAdd(@Comment("Erupt类对象") CustomerInfo model) {
		model.setPhoneNo(model.getPhoneNo().replaceAll("`", ""));
	}

	@Override
	public void beforeUpdate(@Comment("Erupt类对象") CustomerInfo model) {
		String hphm = jdbcTemplate.queryForObject("select car_no from customer_info where id="+ model.getId(),String.class);
		if(StrUtil.isNotBlank(hphm)&&!hphm.equals(model.getCarNo())) {
			int affectNum = jdbcTemplate.update("update car_info set update_time=now(), customer_info_id=null where hphm=?",hphm);
			int affectNumThrid = jdbcTemplate.update("update sms_info set update_time=now(), customer_info_id=null where hphm=?",hphm);
			log.info("CustomerProxy.beforeUpdate carinfo.affect={},sms.affectnum={}",affectNum,affectNumThrid);
		}
	}

	@Override
	public void afterAdd(@Comment("Erupt类对象") CustomerInfo model) {
		String hphm = model.getCarNo();
		if(StrUtil.isNotBlank(hphm)) {			
			int affectNum = jdbcTemplate.update("update car_info set customer_info_id=? where hphm=?",model.getId(),hphm);
			int affectNumThrid = jdbcTemplate.update("update sms_info set update_time=now(), customer_info_id=? where hphm=?",model.getId(),hphm);
			log.info("CustomerProxy.afterAdd carinfo.affect={},sms.affectnum={}",affectNum,affectNumThrid);
		}
	}

	@Override
	public void afterUpdate(@Comment("Erupt类对象") CustomerInfo model) {
		String hphm = model.getCarNo();
		if(StrUtil.isNotBlank(hphm)) {
			int affectNum = jdbcTemplate.update("update car_info set update_time=now(), customer_info_id=? where hphm=?",model.getId(),hphm);
			int affectNumThrid = jdbcTemplate.update("update sms_info set update_time=now(), customer_info_id=? where hphm=?",model.getId(),hphm);
			log.info("CustomerProxy.afterUpdate carinfo.affect={},sms.affectnum={}",affectNum,affectNumThrid);
		}
	}

	@Override
	public void afterDelete(@Comment("Erupt类对象") CustomerInfo model) {
		String hphm = model.getCarNo();
		if(StrUtil.isNotBlank(hphm)) {			
			int affectNum = jdbcTemplate.update("update car_info set update_time=now(), customer_info_id=null where hphm=?",hphm);
			int affectNumThrid = jdbcTemplate.update("update sms_info set update_time=now(), customer_info_id=null where hphm=?",hphm);
			log.info("CustomerProxy.afterDelete carinfo.affect={},sms.affectnum={}",affectNum,affectNumThrid);
		}
	}
		
//	@Override
//	public void excelImport(Object workbook) {
//		
//		HSSFWorkbook wk = (HSSFWorkbook) workbook;
//		
//		HSSFSheet sheet = wk.getSheetAt(0);
//		int columnIndex = 1;
//		for (Row row : sheet) {
//			Cell cell = row.getCell(columnIndex);
//			// 在指定列中执行操作
//            if (cell != null) {
//            	System.err.println(cell.getCellType().getClass().getName());
//            	
//                // 获取单元格的值
//                String cellValue = "";
//                if (cell.getCellType() == CellType.STRING) {
//                    cellValue = cell.getStringCellValue();
//                } else if (cell.getCellType() == CellType.NUMERIC) {
//                    cellValue = String.valueOf(cell.getNumericCellValue());
//                }
//                // 对单元格的值进行操作
//                cell.setCellValue(cellValue + "_modified");
//            }
//		}
//	}
}
